package com.ljt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.admin.entity.SysDict;
import com.ljt.common.utils.BaseResult;
import com.ljt.common.utils.BaseResultGenerator;
import com.ljt.common.utils.annotation.SysLog;
import com.ljt.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author administrator
 * @date 2019/07/25
 * @since 2019/2/1
 */
@Api(value = "字典管理", tags = {"字典管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
public class DictController {
	private final SysDictService sysDictService;

	/**
	 * 通过ID查询字典信息
	 *
	 * @param id ID
	 * @return 字典信息
	 */
	@ApiOperation(value = "通过ID查询", notes = "作者：administrator")
	@ApiImplicitParam(paramType="path", name = "id", value = "对象id", required = true, dataType = "Integer")
	@GetMapping("/{id}")
	public BaseResult getById(@PathVariable Integer id) {
		return BaseResultGenerator.success(sysDictService.getById(id));
	}

	/**
	 * 分页查询字典信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@ApiOperation(value = "分页查询", notes = "作者：administrator")
	@GetMapping("/page")
	public BaseResult<IPage> getDictPage(Page page, SysDict sysDict) {
		return BaseResultGenerator.success(sysDictService.page(page, Wrappers.query(sysDict)));
	}

	/**
	 * 通过字典类型查找字典
	 *
	 * @param type 类型
	 * @return 同类型字典
	 */
	@ApiOperation(value = "通过字典类型查找字典", notes = "作者：administrator")
	@ApiImplicitParam(paramType="path", name = "type", value = "类型", required = true, dataType = "Integer")
	@GetMapping("/type/{type}")
	@Cacheable(value = "dict_details", key = "#type")
	public BaseResult getDictByType(@PathVariable String type) {
		return BaseResultGenerator.success(sysDictService.list(Wrappers
			.<SysDict>query().lambda()
			.eq(SysDict::getType, type)));
	}

	/**
	 * 添加字典
	 *
	 * @param sysDict 字典信息
	 * @return success、false
	 */
	@ApiOperation(value = "添加字典", notes = "作者：administrator")
	@SysLog("添加字典")
	@PostMapping
	@CacheEvict(value = "dict_details", key = "#sysDict.type")
	@PreAuthorize("@pms.hasPermission('sys_dict_add')")
	public BaseResult save(@Valid @RequestBody SysDict sysDict) {
		return BaseResultGenerator.success(sysDictService.save(sysDict));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 *
	 * @param id   ID
	 * @param type 类型
	 * @return R
	 */
	@ApiImplicitParam(paramType="path", name = "type", value = "类型", required = true, dataType = "Integer")
	@ApiOperation(value = "删除字典", notes = "作者：administrator")
	@SysLog("删除字典")
	@DeleteMapping("/{id}/{type}")
	@CacheEvict(value = "dict_details", key = "#type")
	@PreAuthorize("@pms.hasPermission('sys_dict_del')")
	public BaseResult removeById(@PathVariable Integer id, @PathVariable String type) {
		return BaseResultGenerator.success(sysDictService.removeById(id));
	}

	/**
	 * 修改字典
	 *
	 * @param sysDict 字典信息
	 * @return success/false
	 */
	@ApiOperation(value = "修改字典", notes = "作者：administrator")
	@PutMapping
	@SysLog("修改字典")
	@CacheEvict(value = "dict_details", key = "#sysDict.type")
	@PreAuthorize("@pms.hasPermission('sys_dict_edit')")
	public BaseResult updateById(@Valid @RequestBody SysDict sysDict) {
		return BaseResultGenerator.success(sysDictService.updateById(sysDict));
	}
}
